package com.hylanda.va.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.hylanda.va.dao.TIncomeRepository;
import com.hylanda.va.entity.TIncome;
import com.hylanda.va.model.ApiResponse;
import com.hylanda.va.model.IncomeQuery;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年5月29日 下午6:09:15 
 * note
 */
@Service
public class TIncomeService {
	private static Logger logger = LoggerFactory.getLogger(TCostService.class);
	
	@Autowired
	private TIncomeRepository tIncomeRepository;

	public ApiResponse checkFile(MultipartFile file) {
		 //判断文件是否存在
	     if(null == file){
	    	 logger.error("文件不存在！");
	         return new ApiResponse("文件不存在","0");
	     }
	     //获得文件名
	     String fileName = file.getOriginalFilename();
	     //判断文件是否是excel文件
	     if(!fileName.endsWith("xls") && !fileName.endsWith("xlsx")){
	    	 logger.error(fileName + "不是excel文件,请上传excel格式!");
	    	 return new ApiResponse(fileName + "不是excel文件,请上传excel格式!","0");
	     }
	     return null;
	}

	public void checkData(String incomeMonth) {
		List<TIncome> l=tIncomeRepository.findByIncomeMonth(incomeMonth);
		if(l!=null&&l.size()>0){
			tIncomeRepository.deleteByIncomeMonth(incomeMonth);
		}
	}

	public ApiResponse hanlderExcelData(MultipartFile file, String incomeMonth) {
		
		return null;
	}

	public Page<TIncome> findIncomeCriteria(Integer page, Integer size,
			final IncomeQuery incomeQuery) {
		Pageable pageable = new PageRequest(page, size);  
        Page<TIncome> orderPage = tIncomeRepository.findAll(new Specification<TIncome>(){  
            @Override  
            public Predicate toPredicate(Root<TIncome> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {  
                List<Predicate> list = new ArrayList<Predicate>();  
                if(null!=incomeQuery.getContractName()&&!"".equals(incomeQuery.getContractName())){  
                    list.add(criteriaBuilder.equal(root.get("contractName").as(String.class), incomeQuery.getContractName()));  
                }  
                if(null!=incomeQuery.getContractNo()&&!"".equals(incomeQuery.getContractNo())){  
                    list.add(criteriaBuilder.equal(root.get("contractNo").as(String.class), incomeQuery.getContractNo()));  
                }
                if(null!=incomeQuery.getContractSignedMonth()&&!"".equals(incomeQuery.getContractSignedMonth())){  
                    list.add(criteriaBuilder.equal(root.get("contractSignedMonth").as(String.class), incomeQuery.getContractSignedMonth()));  
                }
                if(null!=incomeQuery.getCustomerPopularName()&&!"".equals(incomeQuery.getCustomerPopularName())){  
                    list.add(criteriaBuilder.equal(root.get("customerPopularName").as(String.class), incomeQuery.getCustomerPopularName()));  
                }
                if(null!=incomeQuery.getProductName()&&!"".equals(incomeQuery.getProductName())){  
                    list.add(criteriaBuilder.equal(root.get("productName").as(String.class), incomeQuery.getProductName()));  
                }
                if(null!=incomeQuery.getPersonInCharge()&&!"".equals(incomeQuery.getPersonInCharge())){  
                    list.add(criteriaBuilder.equal(root.get("personInCharge").as(String.class), incomeQuery.getPersonInCharge()));  
                }
                if(null!=incomeQuery.getIncomeMonth()&&!"".equals(incomeQuery.getIncomeMonth())){  
                    list.add(criteriaBuilder.equal(root.get("incomeMonth").as(String.class), incomeQuery.getIncomeMonth()));  
                }
                if(0!=incomeQuery.getSignFixedYear()){  
                    list.add(criteriaBuilder.equal(root.get("signFixedYear").as(Integer.class), incomeQuery.getSignFixedYear()));  
                }
                if(null!=incomeQuery.getSignStatus()&&!"".equals(incomeQuery.getSignStatus())){  
                    list.add(criteriaBuilder.equal(root.get("signStatus").as(String.class), incomeQuery.getSignStatus()));  
                }
                
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));  
            }  
        },pageable);  
        return orderPage;  
	}

	public String genExcel(IncomeQuery incomeQuery) {
		
		return null;
	}
	
}
